/*!
 * @文件名 logic_parse.c
 *
 * @功能描述
 *  通过解析948发来的I2C读写命令来执行主机对屏的控制动作
 */
#include "logic_public.h"
#include "shell.h"

extern ST_VENDOR_DATA gst_vendor_A_data;
extern ST_VENDOR_DATA gst_vendor_B_data;
extern uint8_t	result1,result2;
extern bool black_status;
extern void logWrite(const char *fmt, ...);
void fml_ds90ub948_read_commod(uint8_t *pBuf);
void fml_ds90ub948_wirte_commod(uint8_t *pBuf);

#define MAX_RX_SIZE	100
#define UPDATE_SIZE         2048U

uint8_t pub_slaveRxBuffer[141];
uint8_t pub_slaveTxBuffer[1];
uint16_t packge_index;
uint8_t upgrade_data[UPDATE_SIZE];
int Rx_length = sizeof(pub_slaveRxBuffer);
int Tx_length = sizeof(pub_slaveTxBuffer);
static task_t s_948_task;
static cola_fifo_t g_rx_fifo;
static uint8_t g_rx_buf[MAX_RX_SIZE];


void logic_read_948_task(void *arg, uint32_t event)
{
	(void)arg;
	if(event == 1)
	{//接收底层发来的事件--948 Write
#if 0
		//Here print cause data wrong display
		for(int i=0; i<sizeof(pub_slaveRxBuffer); i++)
			logWrite("0x%02X ",pub_slaveRxBuffer[i]);
		logWrite("\r\n");
#endif
		cola_delay_ms(5);//这个延时必须要,否则切换命令后仍是上一个,原因待查!
		fml_ds90ub948_wirte_commod(pub_slaveRxBuffer);
	}
#if 0
	else
	{
		//Here print better,but wrong display sometimes
		uint8_t buffer[2];
		if(cola_fifo_read(&g_rx_fifo, (uint8_t *)buffer, sizeof(buffer)))
		{
			logWrite("%02X %02X\r\n",buffer[0],buffer[1]);
		}
	}
#endif
}

void logic_parse_init(void)
{
	cola_fifo_init(&g_rx_fifo, g_rx_buf, MAX_RX_SIZE);
	fml_ds90ub948_init(pub_slaveRxBuffer,Rx_length,pub_slaveTxBuffer,Tx_length,(void*)fml_ds90ub948_read_commod,&s_948_task);
	cola_task_create(&s_948_task,logic_read_948_task,NULL);
}

void fml_ds90ub948_wirte_commod(uint8_t *pBuf)
{
	static int pwmValue = -1;
	static int backlightOnOff = -1;
	//cola_fifo_write(&g_rx_fifo, pBuf, 2);
	switch (pBuf[0])
	{
		case LCD_BACKLIGHT_PWM:
			if(pwmValue != pBuf[1])
			{
				pwmValue = pBuf[1];
				fml_set_host_pwm_value(pwmValue);
				fml_pwm_change_duty(pwmValue);
				fml_adc_set_pwm_change_stop(pwmValue);
				//logWrite("rx host pwm=%d\r\n",pwmValue);
			}
			break;

		case LCD_BACKLIGHT_SWITCH:
			if(backlightOnOff != pBuf[1])
			{
				backlightOnOff = pBuf[1];
				if(backlightOnOff == 1)
				{
					fml_max25512_set_bklight_on(true);
				}
				else if(backlightOnOff == 0)
				{
					fml_max25512_set_bklight_on(false);
				}
				//logWrite("backlight=%d\r\n",backlightOnOff);
			}
			break;

		case DAY_NIGHT_MODE:
			break;

		case TOUCH_RESET:
			fml_gpio_write(OUT_TP_RST,true);
			break;

		case UPDATE_WRITE:
			ReceiveData(pBuf);
			if(pBuf[5] == 1)//host require reset
			{
				logWrite("host require reset\r\n ");
				gst_vendor_A_data.host_ack_upgrade = 0x01;
				vendor_data_save();
				SystemSoftwareReset();
			}
			else if(pBuf[5] == 2)//host request mcu mode
			{
				pub_slaveTxBuffer[0] = 0x01;
			}
			break;

		case TIME_STAMP:
			break;

		default:
			fml_ds90ub948_read_commod(pBuf);
			break;
	}
	pBuf[0] = 0x00;
	pBuf[1] = 0x00;
}

void fml_ds90ub948_read_commod(uint8_t *pBuf)
{
	switch(pBuf[0])
	{
		/* code */
		case LCD_BACKLIGHT_FB:
			pub_slaveTxBuffer[0] = fml_get_host_pwm_value();
			break;

		case DIAGNOSTIC_STATUS1:
			pub_slaveTxBuffer[0] =  result1;
			break;

		case DIAGNOSTIC_STATUS2:
			pub_slaveTxBuffer[0] =  result2;
			break;

		case LCD_BACKLIGHT_SWITCH_STATE:
			pub_slaveTxBuffer[0] = black_status;
			break;

		case LCD_TEMPURATURE:
			pub_slaveTxBuffer[0] = fml_adc_convert_to_temperature(fml_get_tft_ntc_adc());
			break;

		case UPDATE_READ:
			//pub_slaveTxBuffer[0] = 0xFA;//接受数据报文回复
			break;

		case LCD_BACKLIGHT_AMBIENT_LIGHT_SENSOR:
			pub_slaveTxBuffer[0] = 0;
			break;

		case I2C_CLASSIFY_VERSION0_READ:
			pub_slaveTxBuffer[0] = APP_CAR_MODLE;
			break;

		case I2C_CLASSIFY_VERSION1_READ:
			pub_slaveTxBuffer[0] = APP_MAJOR_VERSION;
			break;

		case I2C_CLASSIFY_VERSION2_READ:
			pub_slaveTxBuffer[0] = APP_MINOR_VERSION;
			break;

		case I2C_CLASSIFY_VERSION3_READ:
			pub_slaveTxBuffer[0] = APP_SUB_VERSION;
			break;

		case I2C_CLASSIFY_FBL_VERSION0_READ:
			pub_slaveTxBuffer[0] = FBL_MAJOR_VERSION;
			break;

		case I2C_CLASSIFY_FBL_VERSION1_READ:
			pub_slaveTxBuffer[0] = FBL_MINOR_VERSION;
			break;

		case I2C_CLASSIFY_FBL_VERSION2_READ:
			pub_slaveTxBuffer[0] = FBL_SUB_VERSION;
			break;

		case LCD_TYPE:
			pub_slaveTxBuffer[0] = WEIQIER_LCD;
			break;

		case TP_type:
			pub_slaveTxBuffer[0] = WEIQIER_TP;
			break;

		case PN_VERSION0:
			pub_slaveTxBuffer[0] = gst_vendor_A_data.PN_VERSION0;
			break;

		case PN_VERSION1:
			pub_slaveTxBuffer[0] = gst_vendor_A_data.PN_VERSION1;
			break;

		case PN_VERSION2:
			pub_slaveTxBuffer[0] = gst_vendor_A_data.PN_VERSION2;
			break;

		case PN_VERSION3:
			pub_slaveTxBuffer[0] = gst_vendor_A_data.PN_VERSION3;
			break;

		case PN_VERSION4:
			pub_slaveTxBuffer[0] = gst_vendor_A_data.PN_VERSION4;
			break;

		case PN_VERSION5:
			pub_slaveTxBuffer[0] = gst_vendor_A_data.PN_VERSION5;
			break;
		case HARDWARE_VERSION0:
			pub_slaveTxBuffer[0] =  gst_vendor_A_data.HARDWARE_VERSION0;
			break;
		case HARDWARE_VERSION1:
			pub_slaveTxBuffer[0] =  gst_vendor_A_data.HARDWARE_VERSION1;
			break;
		case HARDWARE_VERSION2:
			pub_slaveTxBuffer[0] =  gst_vendor_A_data.HARDWARE_VERSION2;
			break;
		case HARDWARE_VERSION3:
			pub_slaveTxBuffer[0] =  gst_vendor_A_data.HARDWARE_VERSION3;
			break;
		case HARDWARE_VERSION4:
			pub_slaveTxBuffer[0] =  gst_vendor_A_data.HARDWARE_VERSION4;
			break;
		case HARDWARE_VERSION5:
			pub_slaveTxBuffer[0] =  gst_vendor_A_data.HARDWARE_VERSION5;
			break;
		case SOFTWARE_VERSION0:
			pub_slaveTxBuffer[0] = gst_vendor_A_data.SOFTWARE_VERSION0;
			break;
		case SOFTWARE_VERSION1:
			pub_slaveTxBuffer[0] = gst_vendor_A_data.SOFTWARE_VERSION1;
			break;
		case SOFTWARE_VERSION2:
			pub_slaveTxBuffer[0] = gst_vendor_A_data.SOFTWARE_VERSION2;
			break;
		case SOFTWARE_VERSION3:
			pub_slaveTxBuffer[0] = gst_vendor_A_data.SOFTWARE_VERSION3;
			break;
		case SOFTWARE_VERSION4:
			pub_slaveTxBuffer[0] = gst_vendor_A_data.SOFTWARE_VERSION4;
			break;
		case SOFTWARE_VERSION5:
			pub_slaveTxBuffer[0] = gst_vendor_A_data.SOFTWARE_VERSION5;
			break;
		case BOOTLOADER_VERSION:
			pub_slaveTxBuffer[0] = BOOTLOADER_VERSION_V;
			break;
		case I2C_CLASSIFY_FLASH_VERSION_READ:
			pub_slaveTxBuffer[0] = FALSH_VERSION;
			break;
		case DISPLAY_SELF_CHECK_INFO:
			/* code */
			break;
		default:
			break;
	}
	pBuf[0] = 0;
}

void ReceiveData(uint8_t *pBuf)
{
	if(pBuf[7] == 0x04)
	{
		pub_slaveTxBuffer[0] = 0xFA;//接受数据报文回复
		packge_index = pBuf[8]<<8|pBuf[9];
		if(packge_index % 16 == 15)
		{
			for(int i = 0; i<128; i++)
			{
				*((packge_index%16)*128+upgrade_data+i) = *(12+pBuf+i);
			}
			if(gst_vendor_A_data.mcu_upgrade_flag == 0x01)//上一次为A区，往B区写flash
			{
				logWrite("A write to B loading\r\n ");
				INT_SYS_DisableIRQGlobal();
				fml_flash_write_upgrade(B_partion_address+(packge_index-15)*128,upgrade_data,UPDATE_SIZE);//擦写2K
				INT_SYS_EnableIRQGlobal();
			}
			else if(gst_vendor_B_data.mcu_upgrade_flag == 0x01)//上一次为B区，往A区写flash
			{
				logWrite("B write to A loading\r\n ");
				INT_SYS_DisableIRQGlobal();
				fml_flash_write_upgrade(A_partion_address+(packge_index-15)*128,upgrade_data,UPDATE_SIZE);//擦写2K
				INT_SYS_EnableIRQGlobal();
			}
			memset(upgrade_data, 0xFF, sizeof(upgrade_data));
		}
		else
		{
			for(int i = 0; i<128; i++)
			{
				*((packge_index%16)*128+upgrade_data+i) = *(12+pBuf+i);//接收128个字节的升级数据到缓存
			}
		}
		//crc_initial_value = crc16_checksum(crc_initial_value,pBuf+12);
	}
	else if(pBuf[7] == 0x05 && packge_index>200)//校验CRC报文
	{
		if(packge_index % 16 == 15)
		{
			logWrite("boot finish\r\n ");
		}
		else
		{
			INT_SYS_DisableIRQGlobal();
			if(gst_vendor_A_data.mcu_upgrade_flag == 0x01)//上一次为A区，往B区写flash
			{
				fml_flash_write_upgrade(B_partion_address+(packge_index-packge_index%16)*128,upgrade_data,UPDATE_SIZE);
			}
			else if(gst_vendor_B_data.mcu_upgrade_flag == 0x01)//上一次为B区，往A区写flash
			{
				fml_flash_write_upgrade(A_partion_address+(packge_index-packge_index%16)*128,upgrade_data,UPDATE_SIZE);
			}
			INT_SYS_EnableIRQGlobal();
		}
		memset(upgrade_data, 0xFF, sizeof(upgrade_data));
		if(gst_vendor_A_data.mcu_upgrade_flag == 0x01)
		{
			gst_vendor_B_data.mcu_upgrade_flag = 0x01;
			gst_vendor_A_data.mcu_upgrade_flag = 0x00;
		}
		else if(gst_vendor_B_data.mcu_upgrade_flag == 0x01)
		{
			gst_vendor_B_data.mcu_upgrade_flag = 0x00;
			gst_vendor_A_data.mcu_upgrade_flag = 0x01;
		}
		vendor_data_save();
		SystemSoftwareReset();
	}
	else if(pBuf[0] == 0x22)
	{
	}
}


#if 0
int parse(int argc, char *argv[])
{
	if (argc < 3)
	{
		logWrite("para length must greater than 3\r\n");
		return 0;
	}

	int mode = atoi(argv[1]);
	int value = atoi(argv[2]);
	if(mode == 0)
	{
		logWrite("pwm=%d\r\n",value);
		fml_pwm_change_duty(value);
	}
	return 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), parse, parse, parse,parse);
#endif

